<?php
        add_action('wp_head', 'contact_submit');
        function contact_submit(){ ?>
            <script type="text/javascript">
                jQuery(document).ready(function(){
                    function isEmail(emailAddress) {
                        var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
                        return pattern.test(emailAddress);
                    };
                   var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
                   var frm = $('form[name="contact-us"]');
                   $('input[name="txt_name"]').blur(function(){
                       if($(this).val() == ''){
                           $(this).css({border:'1px solid red'});
                       }else{
                           $(this).removeAttr('style');
                       }
                   });
                   $('input[name="txt_email"]').blur(function(){
                       if($(this).val() == ''){
                           $(this).css({border:'1px solid red'});
                       }else{
                           $(this).removeAttr('style');
                       }
                       if(!isEmail($(this).val()) && $(this).val().length > 0){
                           $('span.email_err').css({display:'inline'});
                       }else{
                           $('span.email_err').removeAttr('style');
                       }
                   });
                   $('input[name="txt_subject"]').blur(function(){
                       if($(this).val() == ''){
                           $(this).css({border:'1px solid red'});
                       }else{
                           $(this).removeAttr('style');
                       }
                   });
                   $('textarea[name="txa_message"]').blur(function(){
                       if($(this).val() == ''){
                           $(this).css({border:'1px solid red'});
                       }else{
                           $(this).removeAttr('style');
                       }
                   });
                   $('input[name="6_letters_code"]').blur(function(){
                       if($(this).val() == ''){
                           $(this).css({border:'1px solid red'});
                       }else{
                           $(this).removeAttr('style');
                       }
                   });
                   frm.submit(function(event){
                       var txt_name = $('input[name="txt_name"]');
                       var txt_email = $('input[name="txt_email"]');
                       var txt_subject = $('input[name="txt_subject"]');
                       var txt_message = $('textarea[name="txa_message"]');
                       var txt_captcha = $('input[name="6_letters_code"]');
                       if(txt_name.val().length == 0){
                           txt_name.css({border:'1px solid red'});
                       }else{
                           txt_name.removeAttr('style');
                       }
                       if(txt_email.val().length == 0){
                           txt_email.css({border:'1px solid red'});
                       }else{
                           txt_email.removeAttr('style');
                       }
                       if(txt_subject.val().length == 0){
                           txt_subject.css({border:'1px solid red'});
                       }else{
                           txt_subject.removeAttr('style');
                       }
                       if(txt_message.val().length == 0){
                           txt_message.css({border:'1px solid red'});
                       }else{
                           txt_message.removeAttr('style');
                       }
                       if(txt_captcha.val().length == 0){
                           txt_captcha.css({border:'1px solid red'});
                       }else{
                           txt_captcha.removeAttr('style');
                       }
                       if( txt_name.val().length != 0 
                               && txt_email.val().length != 0 
                               && txt_subject.val().length != 0 
                               && txt_message.val().length != 0
                               && txt_captcha.val().length != 0
                            )
                            {
                                event.preventDefault();
                                var name = txt_name.val();
                                var email = txt_email.val();
                                var subject = txt_subject.val();
                                var issue = $('input:radio[name="rdo_for"]:checked').val();
                                var message = txt_message.val();
                                var captcha = txt_captcha.val();
                                var value_sending = [name,email,subject,issue,message,captcha];
                                var data = {
                                    action : 'contact_action',
                                    value_send : value_sending
                                };
                       }else{
                           return false;
                       }
                       
                       jQuery.post(ajaxurl, data, function(result) {
                           if(result == 'contact_success'){
                               $('div.submit-content').empty();
                               var html ='<div class="row-fluid">';
                                   html +='<p class="thank-you-contact">Thank you! We will get back to you soon!</p>';
                                   html += '</div>';
                                   $('div.submit-content').append(html);
                           }
                           if(result == 'contact_fail'){
                               alert('try again!');
                           }
                           if(result == 'captcha_false'){
                               $('p.captcha_missing').css({display : 'block'});
                           }
                       });
                   });
                });
            </script>
        <?php }
        add_action('wp_ajax_nopriv_contact_action', 'contact_callback');
        add_action('wp_ajax_contact_action', 'contact_callback');
        function contact_callback(){
            global $wpdb,$table_prefix;
            session_start();
            $value_query = $_POST['value_send'];
            $name = $value_query[0];
            $email = $value_query[1];
            $subject = $value_query[2];
            $issue = $value_query[3];
            $message = $value_query[4];
            $captcha = $value_query[5];
            if($_SESSION['6_letters_code'] == $captcha){
                $today = date('Y-m-d h:i:s');
                if($name !='' && $email != '' && $subject != '' && $message != '' && $captcha!=''){
                    $my_query ="
                        INSERT INTO ".$table_prefix."contact(name,email,subject,issue,message,TIME) 
                        VALUE('$name','$email','$subject','$issue','$message','$today')
                    ";
                    $wpdb->get_results($my_query);
                    echo 'contact_success';
                }else{
                    echo 'contact_fail';
                }
            }else{
                echo 'captcha_false';
            }
            die();
        }
?>
